package com.peppa.petshop.controller;


import com.peppa.petshop.entity.Category;
import com.peppa.petshop.service.CategoryService;
import com.peppa.petshop.vo.DataVO;
import com.sun.deploy.cache.BaseLocalApplicationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
//@RestController
@Controller
@RequestMapping(value = "/category")
public class CategoryController {
    @Autowired(required=true)
    private CategoryService categoryService;

    @ResponseBody
    @RequestMapping(value = "/list")
    public DataVO<Category> categoryList(Integer page,Integer limit){
        System.out.println("分类列表");
        return categoryService.categoryList( page, limit);
    }

    @RequestMapping(value = "/list1")
    public ModelAndView categoryList1(Integer page,Integer limit){
        ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/manage/category");
        return modelAndView;
    }

    @GetMapping(value = "/del")
    public String del(Integer id){
        System.out.println("删除类别");
        categoryService.deleteById(id);
        return "redirect:/manage/category.html";
    }
    @PostMapping(value = "/add")
    public String add(@RequestBody Map<String,String> cate){
        System.out.println("添加类别");
        categoryService.addCategory(cate.get("category"));
        return "redirect:/index.html";
    }
    @GetMapping(value = "/edit")
    public String edit(Integer id,String category){
        System.out.println("修改类别");
        categoryService.editCategory(id,category);
        return "redirect:/manage/category/edit.html";
    }

    @ResponseBody
    @PostMapping(value = "/edit")
    public Category editOne(Integer id){
        System.out.println("修改类别");
        Category category = categoryService.getById(id);
        return category;
    }

}

